﻿/*******************************************************************************
 * Copyright © 2017 FriendStar.Framework 版权所有
 * Author: FZH
 * Description: FriendStar快速开发平台
 * Website：http://www.FriendStar.cn
*********************************************************************************/
using System.Collections.Generic;
using FriendStar.Code;
using System.Data.SqlClient;
using FriendStar.Application.VBaseData;
using System.Text;
using System.Data;
using System;
using System.Linq;

namespace FriendStar.Application.WareHouse
{
    /// <summary>
	/// 设备档案对应的数据应用功能类。
	/// </summary>
    public class MaterialStoreApp
    {
        string curUser = OperatorProvider.Provider.GetCurrent().UserName;

        public object GetPrintDetailsData(bool isDefaultDate, bool isArchive, string ids)
        {
            List<Dictionary<string, object>> result = null;

            List<Dictionary<string, object>> pageDataList = null;

            DateTime now = DateTime.Now;

            string sql = @" SELECT  ISNULL(FHappenComName,'') FHappenComName,
	                                ISNULL(FCredence,'') FCredence,
	                                ISNULL(FCurDate,'') FCurDate,
	                                ISNULL(FDescription_DOC,'') FDescription_DOC,
	                                FDocuNO,
	                                MaterialName,
                                    MaterialCode,
                                    MaterialSizes,
	                                FQuantity_KP,
	                                FQuantity,
                                    FPrice,
	                                FMoney,
	                                FStoreName,
                                    FStoreSetNO,
	                                FDescription
		                        FROM
			                        {0}
                WHERE  FDocuNO in({1})", tableName = "V_CLK_42_MX_MaterialProListAcco";

            Dictionary<string, object> item = null;

            result = new List<Dictionary<string, object>>();

            pageDataList = new List<Dictionary<string, object>>();

            if (isArchive)
            {
                if (isDefaultDate)
                {
                    tableName = "V_CLK_44_MX_MaterialProListAcco_Mid";
                }
                else
                {
                    tableName = "V_CLK_46_MX_MaterialProListAcco_Arc";
                }
            }

            sql = string.Format(sql, tableName, ids);


            SqlParameter[] pars = new SqlParameter[] {
                
            };
            using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.GetConnection(), CommandType.Text, sql, pars))
            {
                while (reader.Read())
                {
                    item = new Dictionary<string, object>();

                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        item.Add(reader.GetName(i), reader[i]);
                    }

                    pageDataList.Add(item);
                }
            }

            var query = pageDataList.GroupBy(entity => entity["FDocuNO"]);

            foreach (IGrouping<object, Dictionary<string, object>> info in query)
            {
                List<Dictionary<string, object>> sl = info.ToList();//分组后的集合 

                Dictionary<string, object> pageItem = new Dictionary<string, object>();

                string[] cols = "FDocuNO,FCredence,FHappenComName,FCurDate,FDescription_DOC".Split(',');

                foreach (string col in cols)
                {
                    pageItem.Add(col, sl[0][col]);
                }

                pageItem.Add("curUser", curUser);

                pageItem.Add("curDate", now.ToString("yyyy-MM-dd"));

                pageItem.Add("curTime", now.ToString("HH:mm:ss"));

                pageItem.Add("dataList", sl);

                result.Add(pageItem);
            }

            return result;
        }
    }
}




